<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/11/17
 * Time: 10:03 AM
 */
declare(strict_types=1);


namespace application\module\admin\controller;

use application\module\admin\addon\Property as P;
use wshore\core\Response;

/**
 * Class property
 *
 *
 * @package application\module\admin\controller
 */
class property extends admin
{
    /**
     * 显示页面
     */
    public function index()
    {
        $this->doLayoutAssign();
        $this->display();
    }

    /**
     * 修改配置
     * @param int $id
     * @param string $value
     * @param string $note
     * @return void
     */
    public function update(int $id, string $value, string $note = '')
    {
        $instance = P::getInstance();

        if ($instance->upd($id, $value, $note)) {
            P::getInstance()->effectImmediately();
            Response::success('修改成功', $instance->getInfo($id));
        } else {
            Response::failure('修改失败:' . $instance->getError());
        }
    }

    public function updateComment(int $id, string $note)
    {
        $instance = P::getInstance();
        if ($instance->upd($id, null, strpos($note, '%') !== false ? urldecode($note) : $note)) {
            Response::success('修改成功', $instance->getInfo($id));
        } else {
            Response::failure('修改失败:' . $instance->getError());
        }
    }

    public function updateValue(int $id, string $value)
    {
        $instance = P::getInstance();
        if ($instance->upd($id, strpos($value, '%') !== false ? urldecode($value) : $value, null)) {
            P::getInstance()->effectImmediately();
            Response::success('修改成功', $instance->getInfo($id));
        } else {
            Response::failure('修改失败:' . $instance->getError());
        }
    }

    /**
     * 添加配置
     * @param string $name
     * @param string $value
     * @param string $note
     */
    public function add(string $name, string $value, string $note = '')
    {
        $instance = P::getInstance();
        if ($info = $instance->add($name, $value, $note)) {
            P::getInstance()->effectImmediately();
            Response::success('添加成功', $info);
        } else {
            Response::failure('添加失败');
        }
    }

    /**
     * 删除配置
     * @deprecated
     * @param int $id
     */
    public function delete(int $id)
    {
        Response::failure('禁止删除');# 禁止删除
        if (P::getInstance()->rm($id)) {
            Response::success('删除成功');
        } else {
            Response::failure('删除失败');
        }
    }

    /**
     * 获取配置列表
     */
    public function getlist()
    {
        Response::success('', P::getInstance()->getList());
    }

    /**
     * 配置立即生效
     * @return void
     */
    public function effectImmediately()
    {
        P::getInstance()->effectImmediately() ? Response::success('设置成功') : Response::failure('设置失败');
    }
}